﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Entity = Sunboxsoft.ASMS.Server.Domain.Entity;
using Service = Sunboxsoft.ASMS.Server.Domain.Service;

namespace Sunboxsoft.ASMS.Web.ApiControllers
{
    public class CreateController : ApiController
    {
        [HttpPost]
        public ServiceResult PayItem(Entity.EPayItem @params)
        {
            return new Service.CreateService().CreatePayItem(@params);
        }

        [HttpPost]
        public ServiceResult Package(CreatePackageParams @params)
        {
            Entity.EPackageScope[] scopeArray = new Entity.EPackageScope[1];
            scopeArray[0] = new Entity.EPackageScope();
            return new Service.CreateService().CreatePackage(@params.Package, @params.PriceArray, @params.ItemArray, scopeArray);
        }

        [HttpPost]
        public ServiceResult Business(Entity.EBusiness @params)
        {
            return new Service.CreateService().CreateBusiness(@params);
        }

        [HttpPost]
        public ServiceResult Station(Entity.EStation @params)
        {
            Guid? businessId = WebUtility.CookieUtility.GetBusinessId();
            if (businessId == null)
            {
                return new ServiceResult(1, "不是服务商管理员!");
            }
            @params.BusinessId = businessId.Value;
            return new Service.CreateService().CreateStation(@params);
        }

        [HttpPost]
        public ServiceResult Staff(CreateStaffParams @params)
        {
            Guid? businessId = WebUtility.CookieUtility.GetBusinessId();
            if (businessId == null)
            {
                return new ServiceResult(1, "不是服务商管理员!");
            }
            @params.Staff.BusinessId = WebUtility.CookieUtility.GetBusinessId();
            return new Service.CreateService().CreateStaff(@params.Staff, @params.StaffPositionArray);
        }

        [HttpPost]
        public ServiceResult AdminStaff(Entity.EStaff @params)
        {
            return new Service.CreateService().CreateStaff(@params, null);
        }

        [HttpPost]
        public ServiceResult Material(Entity.EMaterial @params)
        {
            @params.BusinessId = WebUtility.CookieUtility.GetBusinessId();
            return new Service.CreateService().CreateMaterial(@params);
        }

        [HttpPost]
        public ServiceResult Supplier(Entity.ESupplier @params)
        {
            @params.BusinessId = WebUtility.CookieUtility.GetBusinessId();
            return new Service.CreateService().CreateSupplier(@params);
        }

        [HttpPost]
        public ServiceResult Warehouse(Entity.EWarehouse @params)
        {
            @params.BusinessId = WebUtility.CookieUtility.GetBusinessId();
            return new Service.CreateService().CreateWarehouse(@params);
        }

        [HttpPost]
        public ServiceResult ServiceItem(Entity.EServiceItem @params)
        {
            @params.BusinessId = WebUtility.CookieUtility.GetBusinessId();
            return new Service.CreateService().CreateServiceItem(@params);
        }

        [HttpPost]
        public ServiceResult ServiceItemRetain(CreateServiceItemRetainParams @params)
        {
            return new Service.CreateService().CreateServiceItemRetain(@params.ServiceItemRetain, WebUtility.CookieUtility.GetBusinessId().Value, @params.StationIdArray);
        }

        [HttpPost]
        public ServiceResult MaterialRetain(CreateMaterialRetainParams @params)
        {
            return new Service.CreateService().CreateMaterialRetain(@params.MaterialRetain, WebUtility.CookieUtility.GetBusinessId().Value, @params.StationIdArray);
        }

        public class CreatePackageParams
        {
            public Entity.EPackage Package { get; set; }
            public Entity.EPackageItem[] ItemArray { get; set; }
            public Entity.ESalesPrice[] PriceArray { get; set; }
        }

        public class CreatePayItemParams
        {
            public string Code { get; set; }
            public string Name { get; set; }
            public int Type { get; set; }
            public decimal Amount { get; set; }
            public string Description { get; set; }
        }

        public class CreateStaffParams
        {
            public Entity.EStaff Staff { get; set; }
            public Entity.EStaffPosition[] StaffPositionArray { get; set; }
        }

        public class CreateServiceItemRetainParams
        {
            public Entity.EServiceItemRetain ServiceItemRetain { get; set; }
            public Guid[] StationIdArray { get; set; }
        }
        public class CreateMaterialRetainParams
        {
            public Entity.EMaterialRetain MaterialRetain { get; set; }
            public Guid[] StationIdArray { get; set; }
        }
    }
}
